package com.example.basemvvmkotlin.vm

import com.example.basemvvmkotlin.net.MyApiClient
import com.example.basemvvmkotlin.net.NewsListBean
import com.example.basemvvmkotlin.net.UserApiService
import com.example.lib_base.base.http.ApiResponse
import com.example.lib_base.base.util.GsonUtils
import com.example.lib_base.base.vm.BaseRvViewModel

class NewsListVm:BaseRvViewModel(){

    fun getNewsListBean(page:Int){
        println("------------------- 页码$page")
        launchOnIO {
            accessServer (NewsListBean::class) {
                MyApiClient.getInstance().getService(UserApiService::class.java).newsListBean(
                    mutableMapOf("page" to "$page"))
            }
        }
    }

    override fun <T : ApiResponse<*>> serverResponseObj(bean: T, code: Int, message: String) {
        println("------------------  子类接收到数据")
        when(bean.data){
           is NewsListBean ->{
               listLiveData.postValue(GsonUtils.toJson((bean.data as NewsListBean).list))
//               listLiveData.postValue("[]")
            }
        }
    }

    override fun <B : Any> serverError(code: Int, message: String, type: B) {
        super.serverError(code, message, type)

    }
}